package com.baosight.xcyber.tm.re.repository;

import com.alibaba.fastjson.JSONObject;
import com.baosight.xcyber.tm.re.controller.WebSocketServer;
import com.baosight.xcyber.tm.re.domain.UIApplicationInfo;
import com.baosight.xcyber.tm.re.domain.UINode;
import com.baosight.xcyber.tm.re.domain.UITreeNode;
import com.zeroc.IceGrid.ApplicationInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Repository
public class ApplicationRepository {
    public static Logger logger = LoggerFactory.getLogger(ApplicationRepository.class);

    private List<UIApplicationInfo> uiApplicationInfoList=new ArrayList<>();

    public void setUiApplicationInfoList(List<ApplicationInfo> applicationInfoList){
        List<UIApplicationInfo> collect = applicationInfoList.stream().map(
                UIApplicationInfo::new).collect(Collectors.toList());
        uiApplicationInfoList=collect;
    }

    public List<UITreeNode> getUiApplicationInfoList(){
        List<UITreeNode> uiNodesList=new ArrayList<>();
        uiApplicationInfoList.forEach(item->{
            uiNodesList.addAll(item.getChildren());
        });
        return  uiNodesList;
    }

    public void updateUiApplicationInfoList(){
        WebSocketServer.sendAllMessage(JSONObject.toJSONString(getUiApplicationInfoList()));
    }

}
